import functools


class Solution(object):
    def maxJumps(self, arr, d):
        n = len(arr)

        @functools.cache
        def dfs(i):
            max_val = 0
            left_max_index = set()
            ans = 1
            for j in range(i - 1, max(i - d, 0) - 1, - 1):
                if arr[j] < arr[i]:
                    if arr[j] > max_val:
                        max_val = arr[j]
                        left_max_index = {j}
                    else:
                        if arr[j] == max_val:
                            left_max_index.add(j)
                else:
                    break
            if left_max_index:
                for index in left_max_index:
                    ans = max(ans, 1 + dfs(index))
            max_val = 0
            right_max_index = set()
            for j in range(i + 1, min(i + d + 1, n)):
                if arr[j] < arr[i]:
                    if arr[j] > max_val:
                        max_val = arr[j]
                        right_max_index = {j}
                    else:
                        if arr[j] == max_val:
                            right_max_index.add(j)
                else:
                    break
            if right_max_index:
                for index in right_max_index:
                    ans = max(ans, 1 + dfs(index))
            return ans

        max_ans = 0
        for i in range(n):
            max_ans = max(max_ans, dfs(i))
        return max_ans


data = Solution()
arr = [6, 4, 14, 6, 8, 13, 9, 7, 10, 6, 12]
d = 2
print(data.maxJumps(arr, d))
